<script setup>
import gameData, { gameList as allGameList } from '@/common/data';
import { computed, onMounted, ref, watch } from 'vue';
import GameItem from './GItem.vue';
import GameMenus from './GMenus.vue';
import { getQuery } from '@/common/utils';
import { throttle } from 'lodash';

const searchVal = ref('');
const throttleVal = ref('');

const unfold = ref(false);
const gameList = computed(() => {
  return gameData['Recommend'].slice(0, unfold.value ? 999 : 9);
});

const throttleInput = throttle(
  () => {
    searchVal.value = throttleVal.value;
  },
  500,
  { leading: false },
);

watch(throttleVal, () => {
  throttleInput();
});

onMounted(() => {
  window.scrollTo({
    top: 0,
    behavior: 'instant',
  });
});

const searchGames = computed(() => {
  return searchVal.value
    ? allGameList.filter((item) =>
        item.name
          .toLowerCase()
          .replace(/\s+/g, '')
          .includes(searchVal.value.toLowerCase().replace(/\s+/g, '')),
      )
    : [];
});

const getLayoutSize = (index) => {
  const number = index + 1;
  return Math.ceil(number / 6) % 2 ? number % 6 === 1 : number % 6 === 2;
};
</script>
<template>
  <div class="search-page-wrap">
    <div class="search-header">
      <van-button icon="arrow-left" round @click="$router.push(getQuery('/'))" />

      <van-search
        v-model="throttleVal"
        shape="round"
        placeholder="Search Games"
        autocomplete="off"
        left-icon=""
        class="autoSizeSearch"
      />
    </div>

    <div v-if="searchGames.length">
      <div class="recommend-header blue-bg">
        <van-icon name="search" />
        <span>{{ searchGames.length }} result:</span>
      </div>
      <div class="game-list">
        <GameItem
          v-for="(game, index) in searchGames"
          :key="game.name"
          :class="[
            'game-item',
            {
              hide: !game.name || game.hidden,
              large: getLayoutSize(index),
              gap: index === 5,
            },
          ]"
          :game="game"
        />
      </div>
    </div>
    <div v-else-if="searchVal && !searchGames.length" class="result-empty">
      <div>No search results yet</div>
      <div>Try searching for something else?</div>
    </div>

    <GameMenus />

    <div class="recommend-header red-bg">
      <van-icon name="fire-o" />
      <span>Recommend</span>
    </div>
    <div class="game-list">
      <GameItem
        v-for="(game, index) in gameList"
        :key="game.name"
        :class="[
          'game-item',
          {
            hide: !game.name || game.hidden,
            large: getLayoutSize(index),
            gap: index === 5,
          },
        ]"
        :game="game"
      />
    </div>
    <div class="unfold-box" @click="unfold = !unfold">
      <span>{{ unfold ? 'fold' : 'view' }} more</span>
      <van-icon name="arrow-double-right" :class="{ unfolded: unfold }" />
    </div>
  </div>
</template>
<style lang="scss" scoped>
.search-page-wrap {
  position: relative;
  width: 100vw;
  min-height: 100vh;
  overflow-x: hidden;
  color: #fff;

  .search-header {
    height: 70px;
    display: flex;
    align-items: center;

    .van-button {
      border: none;
      background: none;
      font-size: 18px;
      color: #666;

      &::before {
        background: none;
      }

      &:active {
        color: #1989fa;
      }

      .van-icon {
        font-weight: bold;
      }
    }

    .van-search {
      flex: 1;
      height: 43px;
      padding: 0 20px 0 0;
      background: none;
    }
  }

  .recommend-header {
    padding: 4px 14px;
    margin-bottom: 5px;
    font-size: 16px;
    background: linear-gradient(to right, rgb(70, 191, 50, 1), rgba(255, 213, 52, 0));

    &.blue-bg {
      background: linear-gradient(to right, rgb(52, 114, 255, 1), rgba(52, 114, 255, 0));
    }

    &.red-bg {
      background: linear-gradient(to right, rgb(255, 44, 44, 1), rgba(255, 100, 52, 0));
    }

    .van-icon {
      margin-right: 8px;
    }
  }

  .result-empty {
    text-align: center;
    padding: 10px 0 30px;

    div:nth-child(1) {
      font-size: 22px;
      font-weight: 700;
    }

    div:nth-child(2) {
      color: rgba(14, 43, 77, 0.5);
      font-size: 14px;
      font-weight: normal;
      font-family: normal;
    }
  }

  .game-list {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    padding: 14px;

    .advertising {
      grid-column: 1 / -1;
      grid-row: 4 / 5;
      margin: 0 -14px;
    }

    .game-item {
      position: relative;
      display: flex;
      justify-content: center;
      min-height: 109px;

      &.large {
        grid-row: span 2;
        grid-column: span 2;
      }

      &.hide {
        z-index: -1;
        opacity: 0;
      }
    }
  }

  .unfold-box {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 50px;
    opacity: 0.9;

    i {
      position: relative;
      top: 2px;
      left: 3px;
      transition: all 0.2s;
      transform: rotate(90deg);

      &.unfolded {
        transform: rotate(-90deg);
      }
    }
  }
}
</style>
